package leetcode.code0636;

import java.util.Arrays;
import java.util.List;
import java.util.Stack;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int[] exclusiveTime(int n, List<String> logs) {
		int[] ans = new int[n];
		int[][] stack = new int[logs.size() + 1][2];
		for (int i = 0, p = 0; i < logs.size(); i++) {
			String[] _logs = logs.get(i).split(":");
			int id = Integer.valueOf(_logs[0]);
			int status = _logs[1].equals("start") ? 1 : 0;
			int time = Integer.valueOf(_logs[2]);
			if (status == 1) {
				stack[p][0] = time;
				stack[p][1] = 0;
				p++;
			} else {
				int[] s = stack[--p];
				int me = time - s[0] + 1;
				int cur = me - s[1];
				ans[id] += cur;
				if (p != 0) {
					stack[p - 1][1] += me;
				}
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(H.str2array3("[6]"), this.exclusiveTime(1,
				H.str2liststr("[\"0:start:0\",\"0:start:1\",\"0:start:2\",\"0:end:3\",\"0:end:4\",\"0:end:5\"]")));
		H.compare(H.str2array3("[8]"), this.exclusiveTime(1,
				H.str2liststr("[\"0:start:0\",\"0:start:2\",\"0:end:5\",\"0:start:6\",\"0:end:6\",\"0:end:7\"]")));
		H.compare(H.str2array3("[7,1]"), this.exclusiveTime(2,
				H.str2liststr("[\"0:start:0\",\"0:start:2\",\"0:end:5\",\"1:start:6\",\"1:end:6\",\"0:end:7\"]")));
		H.compare(H.str2array3("[3,4]"),
				this.exclusiveTime(2, H.str2liststr("[\"0:start:0\",\"1:start:2\",\"1:end:5\",\"0:end:6\"]")));
		H.compare(H.str2array3("[20,14,35,7,6,9,10,14]"), this.exclusiveTime(8, H.str2liststr(
				"[\"0:start:0\",\"1:start:5\",\"2:start:6\",\"3:start:9\",\"4:start:11\",\"5:start:12\",\"6:start:14\",\"7:start:15\",\"1:start:24\",\"1:end:29\",\"7:end:34\",\"6:end:37\",\"5:end:39\",\"4:end:40\",\"3:end:45\",\"0:start:49\",\"0:end:54\",\"5:start:55\",\"5:end:59\",\"4:start:63\",\"4:end:66\",\"2:start:69\",\"2:end:70\",\"2:start:74\",\"6:start:78\",\"0:start:79\",\"0:end:80\",\"6:end:85\",\"1:start:89\",\"1:end:93\",\"2:end:96\",\"2:end:100\",\"1:end:102\",\"2:start:105\",\"2:end:109\",\"0:end:114\"]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
